package com.example.asus.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;

public class Stuadapter extends RecyclerView.Adapter<StuviewHoder> {
    private Context context;
    private ArrayList<Student>studentArrayList;

    public Stuadapter(Context context, ArrayList<Student> studentArrayList) {
        this.context = context;
        this.studentArrayList = studentArrayList;
    }

    @Override
    public int getItemCount() {
        return studentArrayList.size();
    }
    @Override
    public StuviewHoder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(
                R.layout.list_item,parent,false
        );
        StuviewHoder viewHoder=new StuviewHoder(view);
        return viewHoder;
    }

    @Override
    public void onBindViewHolder( StuviewHoder hoder, int position) {
        /*实现绑定*/
       final Student s=studentArrayList.get(position);

        hoder.headerview.setImageResource(s.getImgId());
        hoder.nameview.setText(s.getName());
        hoder.ageview.setText(String.valueOf(s.getAge()));
        /*点击事件*/
        hoder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,s.getName()+""+s.getAge()+"",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }


}
